<?php
class tree
{

    public function __construct ()
    {
        $this->db = new DB();
    }

    public function extension ($data)
    {
        $qry = $this->db->query("SELECT `id`,`account`,`prev_id`,`acl`,`name` FROM `k_user` WHERE `account`='" . $data[0] . "' LIMIT 1");
        $rlt = mysql_fetch_array($qry);
        if ($data[2] == 1 || $data[2] == 2)
        {
            $data[2] = " AND NOT `enabled`='0' ";
        }
        else
        {
            $data[2] = " AND `enabled`='0' ";
        }
        if (isset($rlt['acl']))
        {
            $str = "";
            switch ($rlt['acl'])
            {
                case 255 :
                    $qry255 = $this->db->query("SELECT `id`,`account`,`prev_id`,`acl`,`name`,`enabled` FROM `k_user` WHERE `acl`>0 and `acl`='5' " . $data[2] . " ORDER BY `account` ASC");
                    $str = "[{\"title\" : \"" . $rlt['account'] . " " . "(" . mysql_affected_rows() . ")" . "\" ,";
                    $str = $str . " \"addClass\" : \"acl255\" ,\"tooltip\": \"" . $rlt['name'] . "\" ,\"children\":[";
                    while ($rlt255 = mysql_fetch_array($qry255))
                    {
                        $qry5 = $this->db->query("SELECT `id`,`account`,`prev_id`,`acl`,`name`,`enabled` FROM `k_user` WHERE `acl`>0 and `prev_id`='" . $rlt255['id'] . "' " . $data[2] . " ORDER BY `account` ASC");
                        $str = $str . "{\"title\" : \"" . $rlt255['account'] . " " . "(" . mysql_affected_rows() . ")" . "\",";
                        $str = $str . " \"addClass\" : \"acl5\" ,\"tooltip\": \"" . $rlt255['name'] . "\" ,\"children\":[";
                        while ($rlt5 = mysql_fetch_array($qry5))
                        {
                            $qry4 = $this->db->query("SELECT `id`,`account`,`prev_id`,`acl`,`name`,`enabled` FROM `k_user` WHERE `acl`>0 and `prev_id`='" . $rlt5['id'] . "' " . $data[2] . " ORDER BY `account` ASC");
                            $str = $str . "{\"title\" : \"" . $rlt5['account'] . " " . "(" . mysql_affected_rows() . ")" . "\",";
                            if ($rlt5['enabled'] == "2")
                            {
                                $str = $str . " \"addClass\" : \"enabled\",\"tooltip\": \"" . $rlt5['name'] . "\" ,\"children\":[";
                            }
                            else
                            {
                                $str = $str . " \"addClass\" : \"acl4\" ,\"tooltip\": \"" . $rlt5['name'] . "\" ,\"children\":[";
                            }
                            while ($rlt4 = mysql_fetch_array($qry4))
                            {
                                $qry3 = $this->db->query("SELECT `id`,`account`,`prev_id`,`acl`,`name`,`enabled` FROM `k_user` WHERE `acl`>0 and `prev_id`='" . $rlt4['id'] . "' " . $data[2] . " ORDER BY `account` ASC");
                                $str = $str . "{\"title\" : \"" . $rlt4['account'] . " " . "(" . mysql_affected_rows() . ")" . "\",";
                                if ($rlt5['enabled'] == "2" || $rlt4['enabled'] == "2")
                                {
                                    $str = $str . " \"addClass\" : \"enabled\",\"tooltip\": \"" . $rlt4['name'] . "\" ,\"children\":[";
                                }
                                else
                                {
                                    $str = $str . " \"addClass\" : \"acl3\" ,\"tooltip\": \"" . $rlt4['name'] . "\" ,\"children\":[";
                                }
                                while ($rlt3 = mysql_fetch_array($qry3))
                                {
                                    $qry2 = $this->db->query("SELECT `id`,`account`,`prev_id`,`acl`,`name`,`enabled` FROM `k_user` WHERE `acl`>0 and `prev_id`='" . $rlt3['id'] . "' " . $data[2] . " ORDER BY `account` ASC");
                                    $str = $str . "{\"title\" : \"" . $rlt3['account'] . " " . "(" . mysql_affected_rows() . ")" . "\",";
                                    if ($rlt5['enabled'] == "2" || $rlt4['enabled'] == "2" || $rlt3['enabled'] == "2")
                                    {
                                        $str = $str . " \"addClass\" : \"enabled\",\"tooltip\": \"" . $rlt3['name'] . "\" ,\"children\":[";
                                    }
                                    else
                                    {
                                        $str = $str . " \"addClass\" : \"acl2\" ,\"tooltip\": \"" . $rlt3['name'] . "\" ,\"children\":[";
                                    }
                                    while ($rlt2 = mysql_fetch_array($qry2))
                                    {
                                        if ($rlt5['enabled'] == "2" || $rlt4['enabled'] == "2" || $rlt3['enabled'] == "2" || $rlt2['enabled'] == "2")
                                        {
                                            $str = $str . "{\"title\" : \"" . $rlt2['account'] . " " . "\",\"addClass\" : \"enabled\"  ,\"tooltip\": \"" . $rlt2['name'] . "\"},";
                                        }
                                        else
                                        {
                                            $str = $str . "{\"title\" : \"" . $rlt2['account'] . " " . "\",\"addClass\" : \"acl1\"  ,\"tooltip\": \"" . $rlt2['name'] . "\"},";
                                        }
                                    }
                                    $str = rtrim($str, ",");
                                    $str = $str . "]},";
                                }
                                $str = rtrim($str, ",");
                                $str = $str . "]},";
                            }
                            $str = rtrim($str, ",");
                            $str = $str . "]},";
                        }
                        $str = rtrim($str, ",");
                        $str = $str . "]},";
                    }
                    $str = rtrim($str, ",");
                    $str = $str . "]}]";
                    break;
                case 5 :
                    $qry5 = $this->db->query("SELECT `id`,`account`,`prev_id`,`acl`,`name`,`enabled` FROM `k_user` WHERE `acl`>0 and `prev_id`='" . $rlt['id'] . "' " . $data[2] . " ORDER BY `account` ASC");
                    $str = "[{\"title\" : \"" . $rlt['account'] . " " . "(" . mysql_affected_rows() . ")" . "\" ,";
                    $str = $str . " \"addClass\" : \"acl5\" ,\"tooltip\": \"" . $rlt['name'] . "\",\"children\":[";
                    while ($rlt5 = mysql_fetch_array($qry5))
                    {
                        $qry4 = $this->db->query("SELECT `id`,`account`,`prev_id`,`acl`,`name`,`enabled` FROM `k_user` WHERE `acl`>0 and `prev_id`='" . $rlt5['id'] . "' " . $data[2] . " ORDER BY `account` ASC");
                        $str = $str . "{\"title\" : \"" . $rlt5['account'] . "( ". $rlt5['name'] . ") " . "(" . mysql_affected_rows() . ")" . "\",";
                        if ($rlt5['enabled'] == "2")
                        {
                            $str = $str . " \"addClass\" : \"enabled\",\"tooltip\": \"" . $rlt5['name'] . "\" ,\"children\":[";
                        }
                        else
                        {
                            $str = $str . " \"addClass\" : \"acl4\" ,\"tooltip\": \"" . $rlt5['name'] . "\",\"children\":[";
                        }
                        while ($rlt4 = mysql_fetch_array($qry4))
                        {
                            $qry3 = $this->db->query("SELECT `id`,`account`,`prev_id`,`acl`,`name`,`enabled` FROM `k_user` WHERE `acl`>0 and `prev_id`='" . $rlt4['id'] . "' " . $data[2] . " ORDER BY `account` ASC");
                            $str = $str . "{\"title\" : \"" . $rlt4['account'] .  "( ". $rlt4['name'] . ") " . "(" . mysql_affected_rows() . ")" . "\",";
                            if ($rlt5['enabled'] == "2" || $rlt4['enabled'] == "2")
                            {
                                $str = $str . " \"addClass\" : \"enabled\",\"tooltip\": \"" . $rlt4['name'] . "\" ,\"children\":[";
                            }
                            else
                            {
                                $str = $str . " \"addClass\" : \"acl3\" ,\"tooltip\": \"" . $rlt4['name'] . "\" ,\"children\":[";
                            }
                            while ($rlt3 = mysql_fetch_array($qry3))
                            {
                                $qry2 = $this->db->query("SELECT `id`,`account`,`prev_id`,`acl`,`name`,`enabled` FROM `k_user` WHERE `acl`>0 and `prev_id`='" . $rlt3['id'] . "' " . $data[2] . " ORDER BY `account` ASC");
                                $str = $str . "{\"title\" : \"" . $rlt3['account'] .  "( ". $rlt3['name'] . ") " . "(" . mysql_affected_rows() . ")" . "\",";
                                if ($rlt5['enabled'] == "2" || $rlt4['enabled'] == "2" || $rlt3['enabled'] == "2")
                                {
                                    $str = $str . " \"addClass\" : \"enabled\",\"tooltip\": \"" . $rlt3['name'] . "\" ,\"children\":[";
                                }
                                else
                                {
                                    $str = $str . " \"addClass\" : \"acl2\" ,\"tooltip\": \"" . $rlt3['name'] . "\",\"children\":[";
                                }
                                while ($rlt2 = mysql_fetch_array($qry2))
                                {
                                    if ($rlt5['enabled'] == "2" || $rlt4['enabled'] == "2" || $rlt3['enabled'] == "2" || $rlt2['enabled'] == "2")
                                    {
                                        $str = $str . "{\"title\" : \"" . $rlt2['account'] .  "( ". $rlt2['name'] . ") " . "\",\"addClass\" : \"enabled\"  ,\"tooltip\": \"" . $rlt2['name'] . "\"},";
                                    }
                                    else
                                    {
                                        $str = $str . "{\"title\" : \"" . $rlt2['account'] .  "( ". $rlt2['name'] . ") " . "\",\"addClass\" : \"acl1\"  ,\"tooltip\": \"" . $rlt2['name'] . "\"},";
                                    }
                                }
                                $str = rtrim($str, ",");
                                $str = $str . "]},";
                            }
                            $str = rtrim($str, ",");
                            $str = $str . "]},";
                        }
                        $str = rtrim($str, ",");
                        $str = $str . "]},";
                    }
                    $str = rtrim($str, ",");
                    $str = $str . "]}]";
                    break;
                case 4 :
                    $qry4 = $this->db->query("SELECT `id`,`account`,`prev_id`,`acl`,`name`,`enabled` FROM `k_user` WHERE `acl`>0 and `prev_id`='" . $rlt['id'] . "' " . $data[2] . " ORDER BY `account` ASC");
                    $str = "[{\"title\" : \"" . $rlt['account'] . " " . "(" . mysql_affected_rows() . ")" . "\" ,";
                    $str = $str . " \"addClass\" : \"acl4\" ,\"tooltip\": \"" . $rlt['name'] . "\",\"children\":[";
                    while ($rlt4 = mysql_fetch_array($qry4))
                    {
                        $qry3 = $this->db->query("SELECT `id`,`account`,`prev_id`,`acl`,`name`,`enabled` FROM `k_user` WHERE `acl`>0 and `prev_id`='" . $rlt4['id'] . "' " . $data[2] . " ORDER BY `account` ASC");
                        $str = $str . "{\"title\" : \"" . $rlt4['account'] . " " . "(" . mysql_affected_rows() . ")" . "\",";
                        if ($rlt4['enabled'] == "2")
                        {
                            $str = $str . " \"addClass\" : \"enabled\",\"tooltip\": \"" . $rlt4['name'] . "\" ,\"children\":[";
                        }
                        else
                        {
                            $str = $str . " \"addClass\" : \"acl3\",\"tooltip\": \"" . $rlt4['name'] . "\" ,\"children\":[";
                        }
                        while ($rlt3 = mysql_fetch_array($qry3))
                        {
                            $qry2 = $this->db->query("SELECT `id`,`account`,`prev_id`,`acl`,`name`,`enabled` FROM `k_user` WHERE `acl`>0 and `prev_id`='" . $rlt3['id'] . "' " . $data[2] . " ORDER BY `account` ASC");
                            $str = $str . "{\"title\" : \"" . $rlt3['account'] . " " . "(" . mysql_affected_rows() . ")" . "\",";
                            if ($rlt4['enabled'] == "2" || $rlt3['enabled'] == "2")
                            {
                                $str = $str . " \"addClass\" : \"enabled\",\"tooltip\": \"" . $rlt3['name'] . "\" ,\"children\":[";
                            }
                            else
                            {
                                $str = $str . " \"addClass\" : \"acl2\" ,\"tooltip\": \"" . $rlt3['name'] . "\",\"children\":[";
                            }
                            while ($rlt2 = mysql_fetch_array($qry2))
                            {
                                if ($rlt4['enabled'] == "2" || $rlt3['enabled'] == "2" || $rlt2['enabled'] == "2")
                                {
                                    $str = $str . "{\"title\" : \"" . $rlt2['account'] . " " . "\",\"addClass\" : \"enabled\"  ,\"tooltip\": \"" . $rlt2['name'] . "\"},";
                                }
                                else
                                {
                                    $str = $str . "{\"title\" : \"" . $rlt2['account'] . " " . "\",\"addClass\" : \"acl1\"  ,\"tooltip\": \"" . $rlt2['name'] . "\"},";
                                }
                            }
                            $str = rtrim($str, ",");
                            $str = $str . "]},";
                        }
                        $str = rtrim($str, ",");
                        $str = $str . "]},";
                    }
                    $str = rtrim($str, ",");
                    $str = $str . "]}]";
                    break;
                case 3 :
                    $qry3 = $this->db->query("SELECT `id`,`account`,`prev_id`,`acl`,`name`,`enabled` FROM `k_user` WHERE `acl`>0 and `prev_id`='" . $rlt['id'] . "' " . $data[2] . " ORDER BY `account` ASC");
                    $str = "[{\"title\" : \"" . $rlt['account'] . " " . "(" . mysql_affected_rows() . ")" . "\" ,";
                    $str = $str . " \"addClass\" : \"acl3\" ,\"tooltip\": \"" . $rlt['name'] . "\",\"children\":[";
                    while ($rlt3 = mysql_fetch_array($qry3))
                    {
                        $qry2 = $this->db->query("SELECT `id`,`account`,`prev_id`,`acl`,`name`,`enabled` FROM `k_user` WHERE `acl`>0 and `prev_id`='" . $rlt3['id'] . "' " . $data[2] . " ORDER BY `account` ASC");
                        $str = $str . "{\"title\" : \"" . $rlt3['account'] . " " . "(" . mysql_affected_rows() . ")" . "\",";
                        if ($rlt3['enabled'] == "2")
                        {
                            $str = $str . " \"addClass\" : \"enabled\",\"tooltip\": \"" . $rlt3['name'] . "\" ,\"children\":[";
                        }
                        else
                        {
                            $str = $str . " \"addClass\" : \"acl2\",\"tooltip\": \"" . $rlt3['name'] . "\" ,\"children\":[";
                        }
                        while ($rlt2 = mysql_fetch_array($qry2))
                        {
                            if ($rlt3['enabled'] == "2" || $rlt2['enabled'] == "2")
                            {
                                $str = $str . "{\"title\" : \"" . $rlt2['account'] . " " . "\",\"addClass\" : \"enabled\"  ,\"tooltip\": \"" . $rlt2['name'] . "\"},";
                            }
                            else
                            {
                                $str = $str . "{\"title\" : \"" . $rlt2['account'] . " " . "\",\"addClass\" : \"acl1\"  ,\"tooltip\": \"" . $rlt2['name'] . "\"},";
                            }
                        }
                        $str = rtrim($str, ",");
                        $str = $str . "]},";
                    }
                    $str = rtrim($str, ",");
                    $str = $str . "]}]";
                    break;
                case 2 :
                    $qry2 = $this->db->query("SELECT `id`,`account`,`prev_id`,`acl`,`name`,`enabled` FROM `k_user` WHERE `acl`>0 and `prev_id`='" . $rlt['id'] . "' " . $data[2] . " ORDER BY `account` ASC");
                    $str = "[{\"title\" : \"" . $rlt['account'] . " " . "(" . mysql_affected_rows() . ")" . " \" ,";
                    $str = $str . " \"addClass\" : \"acl2\" ,\"tooltip\": \"" . $rlt['name'] . "\",\"children\":[";
                    while ($rlt2 = mysql_fetch_array($qry2))
                    {
                        if ($rlt2['enabled'] == "2")
                        {
                            $str = $str . "{\"title\" : \"" . $rlt2['account'] . " " . "\",\"addClass\" : \"enabled\"  ,\"tooltip\": \"" . $rlt2['name'] . "\"},";
                        }
                        else
                        {
                            $str = $str . "{\"title\" : \"" . $rlt2['account'] . " " . "\",\"addClass\" : \"acl1\"  ,\"tooltip\": \"" . $rlt2['name'] . "\"},";
                        }
                    }
                    $str = rtrim($str, ",");
                    $str = $str . "]}]";
                    break;
                case 1 :
                    $str = "[{\"title\" : \"" . $rlt['account'] . " " . "\",\"addClass\" : \"acl1\"  ,\"tooltip\": \"" . str_replace(chr(0), "", $rlt['name']) . "\"}]";
                    break;
            }
            return str_replace(chr(0), "", $str);
        }
    }

}

?>